import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class code2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        PriorityQueue<Long> heap = new PriorityQueue<>();

        while(n--!=0) {
            long x = in.nextInt();
            heap.offer(x);
        }

        //构建最优二叉树 / 构建哈夫曼树
        long ret = 0;
        while(heap.size()>1) {
            long x1 = heap.poll();
            long x2 = heap.poll();
            heap.offer(x1+x2);
            ret += (x1+x2);
        }
        System.out.print(ret);
    }
}